import Reflux from 'reflux';
import Actions from "./Actions";
import update from 'react-addons-update';
import DB from '../../../../app/db';
import StorageUtils from "../../../../app/storage";
import {getInfoByOpenId, RwCode} from "../../toupiao/const/Const"
import Utils from "../../../../utils/index";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    courtId: '',
    userInfo: {},
    court: {},
    rw: {
      CAST01: {
        isLoading: true,
        data: []
      },
      CAST02: {
        isLoading: true,
        data: []
      },
      CAST03: {
        isLoading: true,
        data: []
      },
      CAST04: {
        isLoading: true,
        data: []
      }
    }
  },

  defaultInit(props) {
    this.data.courtId = props.courtId;
    this.data.userInfo = update(this.data.userInfo, {$set: StorageUtils.getUser()});
    this.updateComponent();
    this.initCourtInfo();
    this.initRw();
    // this.getYwhTp();
  },

  initCourtInfo() {
    let t = this;
    DB.WechatAPI.getCourtById({id: this.data.courtId}).then(
      function (content) {
        t.data.court = update(t.data.court, {$set: content});
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      }
    );
  },

  initRw() {
    this.getRw('CAST01');
    this.getRw('CAST02');
    this.getRw('CAST03');
    this.getRw('CAST04');
  },

  initVoteResult(params) {
    let t = this;
    DB.ywh.isVoted({
      proprietorId: params.proprietorId,
      courtId: params.courtId,
      building: params.building,
      unit: params.unit,
      rwCode: params.rwCode
    }).then(
      function (content) {
        if (params.rwCode === 'CAST01') {
          t.data.rw = update(t.data.rw, {
            CAST01: {
              data: {$apply: rws => {
                return rws.map(rw=>{
                  if (rw.unit === params.unit && rw.building === params.building) {
                    return update(rw, {
                      voted: {$set: content.voted},
                      votedDoorId: {$set: content.votedDoorId}
                    })
                  } else {
                    return rw;
                  }
                })
              }}
            }
          });
        } else if (params.rwCode === 'CAST02') {
          t.data.rw = update(t.data.rw, {
            CAST02: {
              data: {$apply: rws => {
                return rws.map(rw=>{
                  if (rw.unit === params.unit && rw.building === params.building) {
                    return update(rw, {
                      voted: {$set: content.voted},
                      votedDoorId: {$set: content.votedDoorId}
                    })
                  } else {
                    return rw;
                  }
                })
              }}
            }
          });
        } else if (params.rwCode === 'CAST03') {
          t.data.rw = update(t.data.rw, {
            CAST03: {
              data: {$apply: rws => {
                return rws.map(rw=>{
                  if (rw.unit === params.unit && rw.building === params.building) {
                    return update(rw, {
                      voted: {$set: content.voted},
                      votedDoorId: {$set: content.votedDoorId}
                    })
                  } else {
                    return rw;
                  }
                })
              }}
            }
          });
        } else if (params.rwCode === 'CAST04') {
          t.data.rw = update(t.data.rw, {
            CAST04: {
              data: {$apply: rws => {
                return rws.map(rw=>{
                  if (rw.unit === params.unit && rw.building === param.building) {
                    return update(rw, {
                      voted: {$set: content.voted},
                      votedDoorId: {$set: content.votedDoorId}
                    })
                  } else {
                    return rw;
                  }
                })
              }}
            }
          });
        } else {
        }
        t.updateComponent();
      },
      function (error) {

      }
    );
  },

  getRw(rwCode) {
    let t = this;
    DB.ywh.getYwhTp02({
      courtId: this.data.courtId,
      proprietorId: t.data.userInfo.proprietorId,
      rwCode: rwCode
    }).then(
      function (content) {
        if (rwCode === 'CAST01') {
          t.data.rw = update(t.data.rw, {
            CAST01: {
              isLoading: {$set: false},
              data: {$set: content}
            }
          });
          t.updateComponent();
          if (content && content.length > 0) {
            content.map(rw => {
              t.initVoteResult({
                proprietorId: t.data.userInfo.proprietorId,
                courtId: t.data.courtId,
                building: rw.building,
                unit: rw.unit,
                rwCode: 'CAST01'
              });
            })
          }
        } else if (rwCode === 'CAST02') {
          t.data.rw = update(t.data.rw, {
            CAST02: {
              isLoading: {$set: false},
              data: {$set: content}
            }
          });
          t.updateComponent();
        } else if (rwCode === 'CAST03') {
          t.data.rw = update(t.data.rw, {
            CAST03: {
              isLoading: {$set: false},
              data: {$set: content}
            }
          });
          t.updateComponent();
        } else if (rwCode === 'CAST04') {
          t.data.rw = update(t.data.rw, {
            CAST04: {
              isLoading: {$set: false},
              data: {$set: content}
            }
          });
          t.updateComponent();
        } else {
          console.log('没有该任务！')
        }
      },
      function (error) {
        if (rwCode === 'CAST01') {
          t.data.rw = update(t.data.rw, {
            CAST01: {
              isLoading: {$set: false},
              data: {$set: []}
            }
          });
        } else if (rwCode === 'CAST02') {
          t.data.rw = update(t.data.rw, {
            CAST02: {
              isLoading: {$set: false},
              data: {$set: []}
            }
          });
        } else if (rwCode === 'CAST03') {
          t.data.rw = update(t.data.rw, {
            CAST03: {
              isLoading: {$set: false},
              data: {$set: []}
            }
          });
        } else if (rwCode === 'CAST04') {
          t.data.rw = update(t.data.rw, {
            CAST04: {
              isLoading: {$set: false},
              data: {$set: []}
            }
          });
        } else {
          console.log('没有该任务！')
        }
        t.updateComponent();
      }
    );
  },

  initYwhVoteResult(rwCode) {

  },

  getFromArray(array, keyValue) {
    let rtn = [];
    if (Utils.arrayIsNotEmpty(array) && Utils.arrayIsNotEmpty(keyValue)) {
      array.map(obj => {
        let isFind = true;
        keyValue.map(kv => {
          if (!(obj.hasOwnProperty(kv.key) && obj[kv.key] === kv.value)) {
            isFind = false;
          }
        });
        if (isFind) {
          rtn.push(obj);
        }
      })
    }
    return rtn;
  },

  // 获取投票任务列表
  getYwhTp() {
    let t = this;
    let courtId = t.data.courtId;
    // 更新门牌号信息
    DB.WechatAPI.getInfoByOpenId({
      openId: t.data.userInfo.openId,
    }).then(
      function (content) {
        console.log(content);
        if (content.length > 0) {
          StorageUtils.setStorage('doors', content);
        } else {
          StorageUtils.removeStorageItem('doors');
        }


      },
      function (error) {
        StorageUtils.removeStorageItem('doors');
      }
    ).catch(function (reason) {
    });

  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState() {
    return this.data;
  }
});
